package com.fr.report.core.barcode.line.codebar;

import com.fr.report.core.barcode.BarcodeException;
import com.fr.report.core.barcode.BarcodeImpl;
import com.fr.report.core.barcode.BlankModule;
import com.fr.report.core.barcode.Module;
import com.fr.report.core.barcode.SeparatorModule;
import java.text.StringCharacterIterator;
import java.util.ArrayList;
import java.util.List;

public class CodabarBarcode extends BarcodeImpl
{
  public static final String DEFAULT_START = "A";
  public static final String DEFAULT_STOP = "C";
  private int width = 0;
  private String label;

  public CodabarBarcode(String paramString)
    throws BarcodeException
  {
    super(paramString);
    this.label = paramString;
    validateData();
  }

  protected String getLabel()
  {
    return this.label;
  }

  protected double getBarcodeWidth(int paramInt)
  {
    encodeData();
    return (this.barWidth * this.width);
  }

  protected Module[] encodeData()
  {
    this.width = 0;
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < this.data.length(); ++i)
    {
      if (i > 0)
      {
        localArrayList.add(new SeparatorModule(1));
        this.width += 1;
      }
      char c = this.data.charAt(i);
      Module localModule = ModuleFactory.getModule(String.valueOf(c));
      localArrayList.add(localModule);
      this.width += localModule.widthInBars();
    }
    return ((Module[])localArrayList.toArray(new Module[0]));
  }

  protected Module calculateChecksum()
  {
    return null;
  }

  protected Module getPreAmble()
  {
    return new BlankModule(0);
  }

  protected Module getPostAmble()
  {
    return new BlankModule(0);
  }

  private void validateData()
    throws BarcodeException
  {
    replaceTraditionalStartStopWithModern();
    addDefaultStartStopIfRequired();
    int i = 0;
    StringBuffer localStringBuffer = new StringBuffer();
    StringCharacterIterator localStringCharacterIterator = new StringCharacterIterator(this.data);
    int j = localStringCharacterIterator.first();
    while (j != 65535)
    {
      if (!(Character.isWhitespace(j)))
      {
        if (!(ModuleFactory.isValid(String.valueOf(j))))
          throw new BarcodeException(j + " is not a valid character for Codabar encoding");
        checkStartStop(j, i);
        localStringBuffer.append(j);
      }
      ++i;
      int k = localStringCharacterIterator.next();
    }
    this.data = localStringBuffer.toString();
  }

  private void addDefaultStartStopIfRequired()
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (!(Character.isLetter(this.data.charAt(0))))
      localStringBuffer.append("A");
    localStringBuffer.append(this.data);
    if (!(Character.isLetter(this.data.charAt(this.data.length() - 1))))
      localStringBuffer.append("C");
    this.data = localStringBuffer.toString();
  }

  private void replaceTraditionalStartStopWithModern()
  {
    this.data = this.data.replace('a', 'A');
    this.data = this.data.replace('t', 'A');
    this.data = this.data.replace('b', 'B');
    this.data = this.data.replace('n', 'B');
    this.data = this.data.replace('c', 'C');
    this.data = this.data.replace('*', 'C');
    this.data = this.data.replace('d', 'D');
    this.data = this.data.replace('e', 'D');
  }

  private void checkStartStop(char paramChar, int paramInt)
    throws BarcodeException
  {
    if ((Character.isLetter(paramChar)) && (paramInt > 0) && (paramInt < this.data.length() - 1))
      throw new BarcodeException(paramChar + " is only allowed as the first and last characters for Codabar barcodes");
  }
}